package com.dormitory.ui;

import com.dormitory.model.Student;

import javax.swing.*;
import java.awt.*;

public class StudentFormDialog extends JDialog {
    private JTextField nameField;
    private JComboBox<String> genderComboBox;
    private JTextField majorField;
    private JTextField classField;
    private JTextField phoneField;
    private JTextField yearField;
    private boolean confirmed = false;
    private Student student;

    public StudentFormDialog(Window owner, Student student) {
        super(owner, "学生信息", ModalityType.APPLICATION_MODAL);
        this.student = student;
        initUI();
        if (student != null) {
            fillForm(student);
        }
        pack();
        setLocationRelativeTo(owner);
    }

    private void initUI() {
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.fill = GridBagConstraints.HORIZONTAL;

        // 姓名
        gbc.gridx = 0; gbc.gridy = 0;
        panel.add(new JLabel("姓名："), gbc);
        gbc.gridx = 1;
        nameField = new JTextField(15);
        panel.add(nameField, gbc);

        // 性别
        gbc.gridx = 0; gbc.gridy = 1;
        panel.add(new JLabel("性别："), gbc);
        gbc.gridx = 1;
        genderComboBox = new JComboBox<>(new String[]{"男", "女"});
        panel.add(genderComboBox, gbc);

        // 专业
        gbc.gridx = 0; gbc.gridy = 2;
        panel.add(new JLabel("专业："), gbc);
        gbc.gridx = 1;
        majorField = new JTextField(15);
        panel.add(majorField, gbc);

        // 班级
        gbc.gridx = 0; gbc.gridy = 3;
        panel.add(new JLabel("班级："), gbc);
        gbc.gridx = 1;
        classField = new JTextField(15);
        panel.add(classField, gbc);

        // 联系电话
        gbc.gridx = 0; gbc.gridy = 4;
        panel.add(new JLabel("联系电话："), gbc);
        gbc.gridx = 1;
        phoneField = new JTextField(15);
        panel.add(phoneField, gbc);

        // 入学年份
        gbc.gridx = 0; gbc.gridy = 5;
        panel.add(new JLabel("入学年份："), gbc);
        gbc.gridx = 1;
        yearField = new JTextField(15);
        panel.add(yearField, gbc);

        // 按钮
        JPanel btnPanel = new JPanel();
        JButton okBtn = new JButton("确定");
        JButton cancelBtn = new JButton("取消");
        okBtn.addActionListener(e -> onOK());
        cancelBtn.addActionListener(e -> onCancel());
        btnPanel.add(okBtn);
        btnPanel.add(cancelBtn);

        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(panel, BorderLayout.CENTER);
        getContentPane().add(btnPanel, BorderLayout.SOUTH);
    }

    private void fillForm(Student s) {
        nameField.setText(s.getStudentName());
        genderComboBox.setSelectedItem(s.getGender());
        majorField.setText(s.getMajor());
        classField.setText(s.getClassName());
        phoneField.setText(s.getContactPhone());
        yearField.setText(String.valueOf(s.getEnrollmentYear()));
    }

    private void onOK() {
        // 校验
        if (nameField.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, "姓名不能为空！");
            return;
        }
        if (majorField.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, "专业不能为空！");
            return;
        }
        if (classField.getText().trim().isEmpty()) {
            JOptionPane.showMessageDialog(this, "班级不能为空！");
            return;
        }
        String phone = phoneField.getText().trim();
        if (!phone.matches("^1[3-9]\\d{9}$") && !phone.isEmpty()) {
            JOptionPane.showMessageDialog(this, "联系电话格式不正确！");
            return;
        }
        int year;
        try {
            year = Integer.parseInt(yearField.getText().trim());
            if (year < 2000 || year > 2100) {
                JOptionPane.showMessageDialog(this, "入学年份不合法！");
                return;
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "入学年份必须为数字！");
            return;
        }
        confirmed = true;
        if (student == null) student = new Student();
        student.setStudentName(nameField.getText().trim());
        student.setGender((String) genderComboBox.getSelectedItem());
        student.setMajor(majorField.getText().trim());
        student.setClassName(classField.getText().trim());
        student.setContactPhone(phone);
        student.setEnrollmentYear(year);
        dispose();
    }

    private void onCancel() {
        confirmed = false;
        dispose();
    }

    public boolean isConfirmed() {
        return confirmed;
    }

    public Student getStudent() {
        return student;
    }
} 